﻿using System;
using System.Windows.Forms;
using CefSharp;
using CefSharp.WinForms;
using WebVideoCollector.CefProcessor;

namespace WebVideoCollector.Form
{
    public partial class MainForm : System.Windows.Forms.Form
    {
        private ChromiumWebBrowser _browser;

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
        }

        private void CreateBrowser(string url)
        {
            _browser = new ChromiumWebBrowser(url)
            {
                Dock = DockStyle.Fill,
            };
            _browser.RequestHandler = new RequestHandler();
            this.panel2.Controls.Add(_browser);

            //_browser.IsBrowserInitializedChanged += Browser_IsBrowserInitializedChanged;

        }

        private void Browser_IsBrowserInitializedChanged(object sender, IsBrowserInitializedChangedEventArgs e)
        {
            if (e.IsBrowserInitialized)
            {
                this._browser.ShowDevTools();
            }
        }

        public void RefreshBrowser()
        {
            _browser.Refresh();
        }

        private void txtUrl_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)//如果输入的是回车键
            {
                CreateBrowser(this.txtUrl.Text.Trim());
            }
        }

    }
}